#include <input_manager.h>#include <draw.h>#include <config.h>#include <stdlib.h>#include <tslib.h>static struct tsdev *g_ptTs;static int g_iXres;static int g_iYres;static int TouchscreenDeviceInit(void){	char *tTsdevice = NULL;

	if( (tTsdevice = getenv("TSLIB_TSDEVICE")) != NULL )
	{
		g_ptTs = ts_open(tTsdevice,0);
	}
	else
	{
		g_ptTs = ts_open("/dev/event1", 0);
	}

	if (!g_ptTs)
	{
		printf("ts_open error\n");
		return -1;
	}

	if (ts_config(g_ptTs)) {
		printf("ts_config error\n");
		return -1;
	}	GetDisplayResolution(&g_iXres, &g_iYres);	
	return 0;}
static int TouchscreenDeviceExit(void){
	return 0;}
static int TouchscreenGetInputEvent(PT_InputEvent ptInputEvent){	struct ts_sample tSamp;	struct ts_sample tSampPressured;	struct ts_sample tSampReleased;	
	int iRet;	int bStart = 0;	int iDeltaX;	tSampPressured.x = -1;	while(1)	{
		
	iRet = ts_read(g_ptTs, &tSamp, 1);		if(iRet == 1)		{			if((tSamp.pressure > 0) && (bStart == 0))			{				tSampPressured = tSamp;				bStart = 1;			}			if(tSamp.pressure <= 0)			{				tSampReleased = tSamp;				if(!bStart)				{					return -1;				}				else				{					iDeltaX = tSampReleased.x - tSampPressured.x;					ptInputEvent->tTime = tSampReleased.tv;					ptInputEvent->iType = INPUT_TYPE_TOUCHSCREEN;										if(iDeltaX > g_iXres/10)					{						ptInputEvent->iVal = INPUT_VALUE_UP;					}					else if(iDeltaX < 0 - g_iXres/10)					{						ptInputEvent->iVal = INPUT_VALUE_DOWN;					}					else					{						ptInputEvent->iVal = INPUT_VALUE_UNKNOWN;					}					return 0;				}			}		}		else		{			return -1;		}
	}	return 0;}

static T_InputOpr g_tTouchscreenInputOpr = {
	.name = "touchscreen",
	.DeviceInit = TouchscreenDeviceInit,
	.DeviceExit = TouchscreenDeviceExit,
	.GetInputEvent = TouchscreenGetInputEvent,};
int TouchscreenRegister(void){	return RegisterInputOpr(&g_tTouchscreenInputOpr);}
